﻿using DHQGHN.DAL.Base;
using DHQGHN.DAL.Common;
using DHQGHN.DAL.Models.Admin;
using DHQGHN.DAL.Models.DHQG;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DHQGHN.DAL.DAO.DHQG
{
    public class DonViDAO : SqlDAO
    {
        public const string PROCEDURE_GET_LIST_PAGING = "spDonVi_GetListPaging";
        public const string PROCEDURE_INSERT_UPDATE = "spDonVi_InsertUpdate";
        public const string PROCEDURE_INSERT_NHOM = "spDonVi_InsertNhom";
        public const string PROCEDURE_INSERT_GROUPS = "spDonVi_InsertGroups";
        public const string PROCEDURE_GET_BY_ID = "spDonVi_GetById";
        public const string PROCEDURE_DELETE = "spDonVi_Delete";
        public const string PROCEDURE_GET_BY_NHOM = "spDonVi_GetAllByNhom";
        public const string PROCEDURE_GET_CO_BAN_BY_NHOM = "spDonVi_GetCoBanByNhom";
        public const string PROCEDURE_GET_BY_TYPE = "spDonVi_GetByType";
        public const string PROCEDURE_GET_LIST_DONVI = "spNguoiDung_Get_List_DonVi";
        public const string PROCEDURE_GET_GROUPS_BY_DONVI_ID = "spGetGroupsByDonViID";

        public const string PARAM_TAI_KHOAN = "@TAI_KHOAN";
        public const string PARAM_DON_VI_ID = "@DON_VI_ID";
        public const string PARAM_NHOM_DON_VI_ID = "@NHOM_DON_VI_ID";
        public const string PARAM_LIST_NHOMID = "@LIST_NHOMID";
        public const string PARAM_LIST_GROUPID = "@LIST_GROUPID";
        public const string PARAM_TEN_DON_VI = "@TEN_DON_VI";
        public const string PARAM_MA_DON_VI = "@MA_DON_VI";
        public const string PARAM_TEN_VIET_TAT = "@TEN_VIET_TAT";
        public const string PARAM_CO_QUYEN_GIAM_SAT = "@CO_QUYEN_GIAM_SAT";
        public const string PARAM_BOOL_CO_QUYEN_GIAM_SAT = "@BOOL_CO_QUYEN_GIAM_SAT";
        public const string PARAM_GHI_CHU = "@GHI_CHU";
        public const string PARAM_PAGE_INDEX = "@PAGE_INDEX";
        public const string PARAM_PAGE_SIZE = "@PAGE_SIZE";
        public DonViDAO(string connectionString, System.Type type)
            : base(connectionString, type) { ;}

        public DataPage<DonVi> GetListPaging(DonVi model, int pageIndex, int pageSize, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_GET_LIST_PAGING);
                cmd.Parameters.Add(CreateParameter(PARAM_NHOM_DON_VI_ID, model.NhomDonViId));
                cmd.Parameters.Add(CreateParameter(PARAM_TEN_DON_VI, model.TenDonVi));
                cmd.Parameters.Add(CreateParameter(PARAM_MA_DON_VI, model.MaDonVi));
                //cmd.Parameters.Add(CreateParameter(PARAM_CO_QUYEN_GIAM_SAT, model.CoQuyenGiamSat));
                cmd.Parameters.Add(CreateParameter(PARAM_CO_QUYEN_GIAM_SAT, model.IntCoQuyenGiamSat));
                //bool boolCoQuyenGiamSat = false;
                //if (model.IntCoQuyenGiamSat == 1)
                //{
                //    boolCoQuyenGiamSat = true;
                //}
                //else if (model.IntCoQuyenGiamSat == 0)
                //{
                //    boolCoQuyenGiamSat = false;
                //}
                //cmd.Parameters.Add(CreateParameter(PARAM_BOOL_CO_QUYEN_GIAM_SAT, boolCoQuyenGiamSat));
                cmd.Parameters.Add(CreateParameter(PARAM_PAGE_INDEX, pageIndex));
                cmd.Parameters.Add(CreateParameter(PARAM_PAGE_SIZE, pageSize));

                return GetDataPage<DonVi>(cmd, pageIndex, pageSize, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        public List<DonVi> GetAllDonViByNhom(int nhomDVID, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_GET_BY_NHOM);
                cmd.Parameters.Add(CreateParameter(PARAM_NHOM_DON_VI_ID, nhomDVID));
                return GetList<DonVi>(cmd, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        public List<DonVi> GetAllDonViCoBanByNhom(int nhomDVID, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_GET_CO_BAN_BY_NHOM);
                cmd.Parameters.Add(CreateParameter(PARAM_NHOM_DON_VI_ID, nhomDVID));
                return GetList<DonVi>(cmd, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        public List<DonVi> GetDonViByType(bool isGiamSat, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_GET_BY_TYPE);
                //int i = isGiamSat ? 1 : 0;
                cmd.Parameters.Add(CreateParameter(PARAM_CO_QUYEN_GIAM_SAT, isGiamSat));
                return GetList<DonVi>(cmd, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        public List<DonVi> GetDonViByUserName(string UserName, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_GET_LIST_DONVI);
                cmd.Parameters.Add(new SqlParameter(PARAM_TAI_KHOAN, UserName));

                return GetList<DonVi>(cmd, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        public bool Save(DonVi model)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_INSERT_UPDATE);
                cmd.Parameters.Add(CreateParameter(PARAM_DON_VI_ID, model.DonViId));
                cmd.Parameters.Add(CreateParameter(PARAM_TEN_VIET_TAT, model.TenVietTat));
                cmd.Parameters.Add(CreateParameter(PARAM_MA_DON_VI, model.MaDonVi));
                cmd.Parameters.Add(CreateParameter(PARAM_TEN_DON_VI, model.TenDonVi));
                //cmd.Parameters.Add(CreateParameter(PARAM_CO_QUYEN_GIAM_SAT, model.CoQuyenGiamSat));
                cmd.Parameters.Add(CreateParameter(PARAM_CO_QUYEN_GIAM_SAT, model.IntCoQuyenGiamSat));
                cmd.Parameters.Add(CreateParameter(PARAM_GHI_CHU, model.GhiChu));

                int result = ExecuteNonQuery(cmd);
                return result > 0 ? true : false;
            }
            catch
            {
                throw;
            }
        }

        public bool SaveNhomDonVi(int DonViId, int NhomDonViId, List<int> NhomDonViKhac)
        {
            try
            {
                DataTable TableTemp = new DataTable();
                TableTemp.Columns.Add("NHOM_DON_VI_ID", typeof(int));
                DataRow workRow;
                foreach (int cat in NhomDonViKhac)
                {
                    workRow = TableTemp.NewRow();
                    workRow["NHOM_DON_VI_ID"] = cat;
                    TableTemp.Rows.Add(workRow);
                }

                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_INSERT_NHOM);
                cmd.Parameters.Add(CreateParameter(PARAM_DON_VI_ID, DonViId));
                cmd.Parameters.Add(CreateParameter(PARAM_NHOM_DON_VI_ID, NhomDonViId));
                //cmd.Parameters.Add(CreateParameter(PARAM_LIST_NHOMID, NhomDonViKhac));

                SqlParameter tvparam = cmd.Parameters.AddWithValue(PARAM_LIST_NHOMID, TableTemp);
                tvparam.SqlDbType = SqlDbType.Structured;

                int result = ExecuteNonQuery(cmd);
                return result > 0 ? true : false;
            }
            catch
            {
                throw;
            }
        }

        public bool SaveGroups(int DonViId, List<int> lstGroupID)
        {
            try
            {
                DataTable TableTemp = new DataTable();
                TableTemp.Columns.Add("NHOM_DON_VI_ID", typeof(int));
                DataRow workRow;
                foreach (int cat in lstGroupID)
                {
                    workRow = TableTemp.NewRow();
                    workRow["NHOM_DON_VI_ID"] = cat;
                    TableTemp.Rows.Add(workRow);
                }

                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_INSERT_GROUPS);
                cmd.Parameters.Add(CreateParameter(PARAM_DON_VI_ID, DonViId));
                SqlParameter tvparam = cmd.Parameters.AddWithValue(PARAM_LIST_GROUPID, TableTemp);
                tvparam.SqlDbType = SqlDbType.Structured;

                int result = ExecuteNonQuery(cmd);
                return result > 0 ? true : false;
            }
            catch
            {
                throw;
            }
        }

        public DonVi GetById(int id, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_GET_BY_ID);
                cmd.Parameters.Add(CreateParameter(PARAM_DON_VI_ID, id));

                return GetSingle<DonVi>(cmd, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        public List<DonVi> GetGroupByDonViID(int intDonViID, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_GET_GROUPS_BY_DONVI_ID);
                cmd.Parameters.Add(CreateParameter(PARAM_DON_VI_ID, intDonViID));

                return GetList<DonVi>(cmd, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        public List<DonVi> GetAll(Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand("[dbo].[spDonVi_GetAll]");

                return GetList<DonVi>(cmd, mapDictionary);
            }
            catch
            {
                
                throw;
            }
        }

        public List<DonVi> GetListDonViInCommentTable(int namKhId, int statusId, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand("spComment_DanhSachDonVi");
                cmd.Parameters.Add(CreateParameter("@NAM_KE_HOACH_ID", namKhId));
                cmd.Parameters.Add(CreateParameter("@STATUS_ID", statusId));

                return GetList<DonVi>(cmd, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        public List<DonVi> GetListDonViDeXuatDieuChinh(int namKhId, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand("spComment_DanhSachCacDonViDeXuatDieuChinh");
                cmd.Parameters.Add(CreateParameter("@NAM_KE_HOACH_ID", namKhId));

                return GetList<DonVi>(cmd, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        public List<DonVi> GetListDonViKhongDeXuatDieuChinh(int namKhId, Dictionary<string, string> mapDictionary)
        {
            SqlCommand cmd = this.GetSprocCommand("spKeHoachThucHien_DanhSachDonViKhongDeXuatDieuChinh");
            cmd.Parameters.Add(CreateParameter("@NAM_KE_HOACH_ID", namKhId));

            return GetList<DonVi>(cmd, mapDictionary);
        }

        //public bool Delete(int id)
        //{
        //    try
        //    {
        //        SqlCommand cmd = this.GetSprocCommand(PROCEDURE_DELETE);
        //        cmd.Parameters.Add(CreateParameter(PARAM_NHOM_DON_VI_ID, id));

        //        int result = ExecuteNonQuery(cmd);
        //        return result > 0 ? true : false;
        //    }
        //    catch 
        //    {
        //        throw;
        //    }
        //}
    }
}
